//
//  RequestService+Plaza.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/27.
//

import UIKit

import Alamofire
import Swime
 
extension RequestService {
    //dataType 1: 首页 2：技能中心
    func presetData(dataType:Int, completeClosure: (([PlazaListModel]?) -> Void)?, failureClosure: ((String?) -> Void)?) {
        let sign = RequestService.instance.generateHash(data: ["systemId":system_id,
                                                               "dataType":"\(dataType)"])
        session.request("\(apiHost)/api/infos/presetData",
                        method: .post,
                        parameters: ["dataType":dataType, "sign":sign], encoding: JSONEncoding.default)
        .responseData { resp in
            self.statRequest(resp)
            switch resp.result{
            case let .success(data):
                let JSON = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                guard let jsonDic = JSON as? [String: Any],
                      let current = jsonDic["code"] as? String, current == "200",
                      let result = jsonDic["result"] as? String else { return }
                if let data = result.data(using: .utf8) {
                    let JSON = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                    if let jsonDic = JSON as? [[String: Any]], let models = [PlazaListModel].deserialize(from: jsonDic) {
                        completeClosure?(models.compactMap { $0 })
                    }else {
                        completeClosure?(nil)
                    }
                }
                completeClosure?(nil)
            case let .failure(error):
                failureClosure?(error.localizedDescription)
                XLog.d("getActivityMsg", "sync fail", error)
            }
        }
    }
    
}
